import BasicTool from '../BasicTool';

/*
绘制圆
 */
export default class DrawCircle {
    constructor(viewer) {
        this.viewer = viewer;
        this._drawHandler = null;
        this._circle = null; //活动圆
        this.floatingPoint = null;
        this._circleLast = null; //最后一个圆
        this._positions = [];  //活动点
        this._entities_point = [];  //脏数据
        this._entities_circle = [];  //脏数据
        this._circleData = null; //用于构造圆形数据
        this.callbackResult = {}
        this.callback = null;
        this.basicTool = new BasicTool(this.viewer)
    }

    getCircle() {
        return this._circleLast;
    }

    //加载圆
    createCircle(data) {
        var position = data[0];
        var value = data;
        var r = Math.sqrt(
            Math.pow(value[0].x - value[value.length - 1].x, 2) +
            Math.pow(value[0].y - value[value.length - 1].y, 2)
        );
        var shape = this.viewer.entities.add({
            position: position,
            name: "circle",
            type: "circle",
            ellipse: {
                semiMinorAxis: r,
                semiMajorAxis: r,
                material: Cesium.Color.RED.withAlpha(0.5),
                outline: true
            }
        });
        return shape;
    }

    //返回数据
    getData() {
        return this._circleData;
    }

    activate(callback) {
        this.callback = callback;
        this._drawHandler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
        this.viewer.scene.globe.depthTestAgainstTerrain = true;
        this._drawHandler.setInputAction((evt) => { //单机开始绘制
            this.viewer.scene.globe.depthTestAgainstTerrain = true;
            //屏幕坐标转地形上坐标
            var cartesian = this.getCatesian3FromPX(evt.position);
            if (this._positions.length == 0) {
                this._positions.push(cartesian.clone());
                // this.floatingPoint = this.createPoint(cartesian);
            }
            // if (!this._circle) {
            //     this.createPoint(cartesian);// 绘制点
            // }
            this._positions.push(cartesian);
        }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
       this._drawHandler.setInputAction((evt) => { //移动时绘制圆
            if (this._positions.length < 1) return;
            var cartesian = this.viewer.scene.pickPosition(evt.endPosition);
            if (!Cesium.defined(this._circle)) {
                this._circle = this.createCircle();
            }
            // this.floatingPoint.position.setValue(cartesian);
            if (this._circle) {
                this._positions.pop();
                this._positions.push(cartesian);
            }
        }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
       this._drawHandler.setInputAction((evt) => {
            if (!this._circle) return;
            this._removeAllEvent();
            this.viewer.scene.globe.depthTestAgainstTerrain = false;
            var cartesian = this.viewer.scene.pickPosition(evt.position);
            this._positions.pop();
            this._positions.push(cartesian);
            this._circleData = this._positions.concat();
            this.viewer.entities.remove(this._circle); //移除
            this._circle = null;
            // this.floatingPoint.position.setValue(cartesian);
            var circle = this.createCircle(this._circleData); //加载
            this._circleLast = circle;
            // this.clearPoint();
            if(typeof this.callback=="function"){
                 // 转换为经纬度坐标
                 var points = this._circleData.map(r=>this.basicTool.cartesian2ArrayLonLat(r))
                 // 回调
                this.callbackResult = {
                     type: "Circle",
                     state: "finish",
                     points: points,
                     postions: this._circleData,
                 }
                this.callback(this.callbackResult);
            }
        }, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
    }

    //创建圆
    createCircle() {
        var shape = this.viewer.entities.add({
            position: this._positions[0],
            name: "circle",
            type: "circle",
            ellipse: {
                semiMinorAxis: new Cesium.CallbackProperty(() => {
                    //半径 两点间距离
                    var r = Math.sqrt(
                        Math.pow(this._positions[0].x - this._positions[this._positions.length - 1].x, 2) +
                        Math.pow(this._positions[0].y - this._positions[this._positions.length - 1].y, 2)
                    );
                    return r ? r : r + 1;
                }, false),
                semiMajorAxis: new Cesium.CallbackProperty(() => {
                    var r = Math.sqrt(
                        Math.pow(this._positions[0].x - this._positions[this._positions.length - 1].x, 2) +
                        Math.pow(this._positions[0].y - this._positions[this._positions.length - 1].y, 2)
                    );
                    return r ? r : r + 1;
                }, false),
                material: Cesium.Color.RED.withAlpha(0.5),
                outline: true
            }
        });
        this._entities_circle.push(shape);
        return shape;
    }

    //创建点
    createPoint(cartesian) {
        var point = this.viewer.entities.add({
            position: cartesian,
            point: {
                pixelSize: 10,
                color: Cesium.Color.YELLOW,
            }
        });;
        this._entities_point.push(point);
        return point;
    }

    getCatesian3FromPX(px) {
        var cartesian;
        var ray = this.viewer.camera.getPickRay(px);
        if (!ray) return null;
        cartesian = this.viewer.scene.globe.pick(ray, this.viewer.scene);
        return cartesian;
    }

    clearPoint(){
        for (var i = 0; i < this._entities_point.length; i++) {
            this.viewer.entities.remove(this._entities_point[i]);
        }
        this._entities_point = [];  //脏数据
    }

    //清空实体对象
    clearAll() {
        this._removeAllEvent()
        this._resetParams()
    }

    /**
     * 重置所有参数
     * @private
     */
    _resetParams() {
        for (var i = 0; i < this._entities_point.length; i++) {
            this.viewer.entities.remove(this._entities_point[i]);
        }
        for (var i = 0; i < this._entities_circle.length; i++) {
            this.viewer.entities.remove(this._entities_circle[i]);
        }
        this._circle = null; //活动圆
        this.floatingPoint = null;
        this._circleLast = null; //最后一个圆
        this._positions = [];  //活动点
        this._entities_point = [];  //脏数据
        this._entities_circle = [];  //脏数据
        this._circleData = null; //用于构造圆形数据
    }

    /**
     * 移除所有鼠标事件
     * @private
     */
    _removeAllEvent() {
        this._drawHandler && (
            this._drawHandler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK),
            this._drawHandler.removeInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE),
            this._drawHandler.removeInputAction(Cesium.ScreenSpaceEventType.RIGHT_CLICK),
            this._drawHandler.destroy(),
            this._drawHandler = null
        )
    }
}
